using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class OutCategoryDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        public List<OutCategory> GetOutCategoryList(int catetype, int catstt)
        {
            try
            {
                const string spName = "spi_OutCategory_GetListByCatType";

                var sqlParams = new[]
                                    {
										new SqlParameter("@CatType", catetype),
                                        new SqlParameter("@CatStatus", catstt)
                                    };
                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);

                var OutCategoryList = DbHelper.ExecuteSqlList<OutCategory>(sqlCommand);
                return OutCategoryList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<OutCategory>();
            }
        }

        public List<OutCategory> GetOutCategoryChildList(int cateParentId, int catstt)
        {
            try
            {
                const string spName = "spi_OutCategory_GetChildListByCatType";

                var sqlParams = new[]
                                    {
										new SqlParameter("@CatParentId", cateParentId),
                                        new SqlParameter("@CatStatus", catstt)
                                    };
                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);

                var OutCategoryList = DbHelper.ExecuteSqlList<OutCategory>(sqlCommand);
                return OutCategoryList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<OutCategory>();
            }
        }
        //public List<OutCategory> GetOutCategoryListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        //{
        //    try
        //    {
        //        const string spName = "spo_OutCategory_GetByPaging";
        //        var sqlParams = new[]
        //                            {
        //                                new SqlParameter("@PageIndex", pageIndex),
        //                                new SqlParameter("@PageSize", pageSize),
        //                                new SqlParameter("@TotalRecord", totalRecord)
        //                            };

        //        SqlCommand sqlCommand = new SqlCommand(spName);
        //        sqlCommand.CommandType = CommandType.StoredProcedure;
        //        sqlCommand.CommandText = spName;
        //        sqlCommand.Parameters.AddRange(sqlParams);
        //        sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
        //        var OutCategoryList = DbHelper.ExecuteSqlList<OutCategory>(sqlCommand);
        //        if (sqlCommand.Parameters["@TotalRecord"].Value != null)
        //        {
        //            totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
        //        }

        //        return OutCategoryList;
        //    }
        //    catch (Exception exp)
        //    {
        //        Logger.WriteLog(Logger.LogType.Error, exp.ToString());

        //        return new List<OutCategory>();
        //    }
        //}

        //public OutCategory GetOutCategoryById(int catId)
        //{
        //    try
        //    {
        //        const string spName = "spo_OutCategory_GetById";
        //        var sqlParams = new[]
        //                            {
        //                                new SqlParameter("@CatId", catId)
        //                            };

        //        var OutCategory = DbHelper.ExecuteSPList<OutCategory>(spName, sqlParams).SingleOrDefault();
        //        return OutCategory;
        //    }
        //    catch (Exception exp)
        //    {
        //        Logger.WriteLog(Logger.LogType.Error, exp.ToString());

        //        return null;
        //    }
        //}

        public int UpdateOutCategory(OutCategory OutCategory)
        {
            try
            {
                const string spName = "spi_OutCategory_Update";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@CatId", OutCategory.CatId),
                                        new SqlParameter("@CatParentId", OutCategory.CatParentId),
                                        new SqlParameter("@CatName", OutCategory.CatName),
                                        new SqlParameter("@CatStatus", OutCategory.CatStatus),
                                        new SqlParameter("@CatType", OutCategory.CatType),
                                        new SqlParameter("@CatIdOut", OutCategory.CatId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@CatIdOut"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@CatIdOut"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@CatIdOut"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        //public int UpdateOutCategory(OutCategory OutCategory)
        //{
        //    try
        //    {
        //        const string spName = "spo_OutCategory_Update";
        //        var sqlParams = new[]
        //                            {
        //                                new SqlParameter("@CatId", OutCategory.CatId)
        //                                , new SqlParameter("@CatParentId", OutCategory.CatParentId)
        //                                , new SqlParameter("@CatName", OutCategory.CatName)
        //                                , new SqlParameter("@CatStatus", OutCategory.CatStatus)
        //                                , new SqlParameter("@CatOrder", OutCategory.CatOrder)
        //                                , new SqlParameter("@CatType", OutCategory.CatType)
        //                            };

        //        var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

        //        return returnVal;
        //    }
        //    catch (Exception exp)
        //    {
        //        Logger.WriteLog(Logger.LogType.Error, exp.ToString());

        //        return -1;
        //    }
        //}

        public int DeleteOutCategory(int catId, int catType)
        {
            try
            {
                const string spName = "spi_OutCategory_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@CatId", catId),
                                    new SqlParameter("@CatType", catType)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        public int OrderOutCategory(int catId, int catParent, int catType, int action)
        {
            try
            {
                int temp = -1;
                const string spName = "spi_OutCategory_Order";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@CatId", catId),
                                        new SqlParameter("@CatParentId", catParent),
                                        new SqlParameter("@CatType", catType),
                                        new SqlParameter("@Action", action),
                                        new SqlParameter("@Return", temp)

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@Return"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@Return"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@Return"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

		#endregion
    }
}
